package com.fancy.learncenter.net;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.fancy.learncenter.bean.BaseBean;
import com.fancy.learncenter.common.Constant;
import com.fancy.learncenter.ui.activity.LoginActivity;
import com.fancy.learncenter.utils.LogUtil;
import com.fancy.learncenter.utils.ToastUtil;


/**
 * Created by Hyy on 2016/12/16.
 */

public class CustomNetCallBack<T> {
    BaseBean<T> baseBean;
    Activity context;


    public CustomNetCallBack(Activity context, BaseBean<T> baseBean) {
        this.baseBean = baseBean;
        this.context = context;
    }

    public void dealData() {
        if (baseBean != null) {
            if (baseBean.getInfo().isOk()) {
                callBack.success(baseBean.getData());
            } else {
                callBack.fail(baseBean.getInfo().getErrorCode());

                if (!TextUtils.isEmpty(baseBean.getInfo().getErrorCode()) && baseBean.getInfo().getErrorCode().equals(Constant.ERROR_LOGIN_CODE)) {
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivityForResult(intent, Constant.FRESH_CALLBACK);
                }

                if (!TextUtils.isEmpty(baseBean.getInfo().getMessage())) {
                    ToastUtil.show(baseBean.getInfo().getMessage());
                }
            }
        } else {

        }
    }


    public void customDealData() {
        if (baseBean != null) {
            if (baseBean.getInfo().isOk()) {
                callBack.success(baseBean.getData());
            } else {
                callBack.fail(baseBean.getInfo().getErrorCode());
            }
        } else {

        }
    }


    private NetCallBack callBack;

    public interface NetCallBack<T> {
        abstract void success(T dataBean);

        abstract void fail(String errorCode);
    }

    public CustomNetCallBack setCallBack(NetCallBack callBack) {
        this.callBack = callBack;
        return this;
    }
}
